package com.cskaoyan.gateway.controller.shopping;

import com.cskaoyan.gateway.form.cart.CartForm;
import com.mall.commons.result.ResponseData;
import com.mall.commons.result.ResponseUtil;
import com.mall.commons.tool.utils.CookieUtil;
import com.mall.shopping.ICartService;
import com.mall.shopping.constants.ShoppingRetCode;
import com.mall.shopping.dto.*;
import com.mall.user.annotation.Anoymous;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * @author ZhaoJiachen on 2021/5/22
 * <p>
 * Description:购物车相关
 * 1. 获得购物车列表
 */

@Slf4j
@RestController
@RequestMapping("/shopping")
@Api(tags = "CartsController", description = "购物车控制层")
public class CartsController {

    public static String USER_INFO_KEY = "userInfo";

    @Reference(timeout = 3000, check = false)
    ICartService cartService;

    @GetMapping("/carts")
    public ResponseData queryCarts(HttpServletRequest request) {
        // 从cookie中获取token
        String userInfo = String.valueOf(request.getAttribute(USER_INFO_KEY));

        // 转发给服务
        CartListByIdRequest cartListByIdRequest = new CartListByIdRequest();
        cartListByIdRequest.setUserId(Long.parseLong(userInfo));

        CartListByIdResponse cartListByIdResponse = cartService.getCartListById(cartListByIdRequest);

        if (!cartListByIdResponse.getCode().equals(ShoppingRetCode.SUCCESS.getCode())) {
            return new ResponseUtil<>().setErrorMsg(cartListByIdResponse.getMsg());
        }
        return new ResponseUtil<>().setData(cartListByIdResponse.getCartProductDtos());
    }

    @PutMapping("/carts")
    @ApiOperation("更新购物车中的商品")
    public ResponseData updateCarts(@RequestBody CartForm cartForm) {
        // request
        // UpdateCartNumRequest
        UpdateCartNumRequest request = new UpdateCartNumRequest();
        request.setChecked(cartForm.getChecked());
        request.setNum(cartForm.getProductNum());
        request.setItemId(cartForm.getProductId());
        request.setUserId(cartForm.getUserId());

        // UpdateCartNumResponse
        UpdateCartNumResponse response =
                cartService.updateCartNum(request);


        if (response.getCode().equals(ShoppingRetCode.SUCCESS.getCode()))//000000
            return new ResponseUtil<>().setData(response.getMsg());
        return new ResponseUtil<>().setErrorMsg(response.getMsg());
    }

    /**
     * panda 删除用户购物车商品
     * @param userId 用户id
     * @param itemId 商品id
     * @return 响应信息
     */
    @DeleteMapping("/carts/{uid}/{pid}")
    @ApiOperation("删除购物车中的商品")
    public ResponseData deleteCarts(@PathVariable("uid") Long userId,@PathVariable("pid") Long itemId){
        //用户id和商品id
        DeleteCartItemRequest deleteCartItemRequest = new DeleteCartItemRequest();
        deleteCartItemRequest.setUserId(userId);
        deleteCartItemRequest.setItemId(itemId);
        DeleteCartItemResponse response = cartService.deleteCartItem(deleteCartItemRequest);
        if (response.getCode().equals(ShoppingRetCode.SUCCESS.getCode()))
            return new ResponseUtil<>().setData(response.getMsg());
        return new ResponseUtil<>().setErrorMsg(response.getMsg());

    }


    @PostMapping("carts")
    public ResponseData addToCarts(@RequestBody Map<String,Object> map, HttpServletRequest httpServletRequest) {

        AddCartRequest addCartRequest = new AddCartRequest();
        addCartRequest.setUserId(Long.valueOf((String)map.get("userId")));
        addCartRequest.setProductId(Long.valueOf((Integer)map.get("productId")));
        addCartRequest.setProductNum((Integer)map.get("productNum"));
        AddCartResponse response = cartService.addToCart(addCartRequest);
        if (!response.getCode().equals(ShoppingRetCode.SUCCESS.getCode())) {
            return new ResponseUtil<>().setErrorMsg(response.getMsg());
        }
        return new ResponseUtil<>().setData(response.getMsg());
    }
}
